/* eslint-disable promise/param-names */
<template>
  <div class="info-container">
    <el-card style="height: 100%">
      <search-tool @onSearch="handleSearch" :rowData="sele"></search-tool>
      <div class="btns">
        <div class="add-btn" @click="handleAdd">
          <svg-icon icon="add" />
          <span class="addFont">新增</span>
        </div>
      </div>
      <el-table v-loading="loading" :data="tableData" style="width: 100%">
        <el-table-column label="序号" type="index" width="80" fixed="left" />
        <el-table-column prop="name" label="角色名称" fixed="left">
        </el-table-column>
        <el-table-column prop="description" label="描述" width="300">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"> </el-table-column>
        <el-table-column prop="updateTime" label="更新时间"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              type="text"
              size="small"
              @click="onDistributePermissionClick(row)"
              style="color: #0052d9"
              >分配权限</el-button
            >
            <div class="has-more"></div>
            <el-button
              type="text"
              size="small"
              @click="handleEdit(row)"
              style="color: #0052d9"
              >修改</el-button
            >
            <div class="has-more"></div>
            <el-button
              type="text"
              size="small"
              @click="handleView(row)"
              style="color: #e34d59"
              >删除</el-button
            >
            <!-- <el-button type="text" size="small" @click="handleView(row)" style="color:#0052D9;">分配数据权限</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 20]"
        :page-size="size"
        layout="prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <AddDialog
      :title="title"
      :rowData="curRowData"
      v-model:visible="visibleViewDialog"
      @onReload="reloadPage"
    ></AddDialog>
    <permission
      :title="title"
      :rowData="curRowData"
      v-model:visible="visiblepermission"
      @onReload="reloadPage"
    >
    </permission>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { role, delerole } from '@/api/role'
import SearchTool from './components/SearchTool/index.vue'
import AddDialog from './components/AddDialog/index.vue'
import permission from './components/permission/index.vue'

import { ElMessage, ElMessageBox } from 'element-plus'
//数据相关
const tableData = ref([])
const total = ref(0)
const page = ref(1)
const size = ref(10)
const params = ref({})
const loading = ref(false)
const title = ref('新增角色')
const value1 = ref()
const sele = ref([])
const curRowData = ref({})
const visibleViewDialog = ref(false)
const visiblepermission = ref(false)
// 所属角色数据
// const role = async() => {
//  const res = await roleList()
//  sele.value = res
// }
// role()

const getListData = async (params) => {
  loading.value = true
  const result = await role(params, page.value, size.value)
  loading.value = false
  tableData.value = result.rows
  total.value = result.total
}

const reloadPage = () => {
  getListData(params.value)
  if (visibleViewDialog.value) {
    visibleViewDialog.value = false
  } else if (visiblepermission.value) {
    visiblepermission.value = false
  }
}

reloadPage()
//查询条件相关
const handleSearch = (paramsData) => {
  params.value = paramsData
  getListData(paramsData)
}

// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getListData(params.value)
}

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getListData(params.value)
}

// 新增用户
const handleAdd = () => {
  title.value = '新增角色'
  visibleViewDialog.value = true
}
// 修改用户
const handleEdit = (row) => {
  console.log(row)
  title.value = '修改角色'
  visibleViewDialog.value = true
  curRowData.value = row
}
// 刪除用戶
const handleView = (row) => {
  ElMessageBox.confirm('确定删除此用户吗？', '操作', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await delerole(row.id).then(() => {
        ElMessage({
          type: 'success',
          message: '删除成功!'
        })
        reloadPage()
      })
    })
    .catch(() => {
      return false
    })
}
// 分配权限
const onDistributePermissionClick = (row) => {
  visiblepermission.value = true
  curRowData.value = row
  title.value = '配置角色'
}
</script>

<style lang="scss" scoped>
.info-container {
  width: 100%;
  .add-btn {
    width: 74px;
    height: 36px;
    background: #1e6ae7;
    border: 1px solid #8bb7ff;
    position: relative;
    margin-bottom: 24px;
    cursor: pointer;
    .addFont {
      position: absolute;
      font-size: 14px;
      font-family: Microsoft YaHei-Regular, Microsoft YaHei;
      font-weight: 400;
      color: #ffffff;
      top: 7px;
      left: 20px;
      cursor: pointer;
    }
  }
  .btns {
    position: relative;
    width: 100%;
    display: flex;
    height: 40px;
    align-items: center;
    margin-bottom: 20px;
  }
  .has-more {
    display: inline-block;
    width: 2px;
    height: 10px;
    background: #0052d9;
    margin-right: 10px;
    margin-left: 10px;
    margin-top: 5px;
  }
  .pagination {
    width: 100%;
    height: 32px;
    margin-top: 20px;
    display: flex;
    align-items: center;
    justify-content: end;
  }
}
</style>
